package org.bambrikii.examples.rutext.main;

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.bambrikii.examples.rutext.lexer.custom.SentenceLexer;
import org.bambrikii.examples.rutext.parser.DefaultParser;
import org.bambrikii.examples.rutext.parser.IParser;
import org.bambrikii.examples.rutext.parser.ParserException;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class SentenceParserWindow {

	private JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					SentenceParserWindow window = new SentenceParserWindow();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public SentenceParserWindow() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 955, 580);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(
				new FormLayout(new ColumnSpec[] { FormFactory.LABEL_COMPONENT_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC, ColumnSpec.decode("default:grow"),
						FormFactory.LABEL_COMPONENT_GAP_COLSPEC, FormFactory.LABEL_COMPONENT_GAP_COLSPEC, },
						new RowSpec[] { FormFactory.LINE_GAP_ROWSPEC, RowSpec.decode("default:grow"),
						FormFactory.LINE_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC, RowSpec.decode("default:grow"),
						FormFactory.LINE_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC,
						FormFactory.LINE_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, }));

		JLabel lblFrom = new JLabel("from");
		frame.getContentPane().add(lblFrom, "2, 2");

		JScrollPane scrollPane = new JScrollPane();
		frame.getContentPane().add(scrollPane, "4, 2, fill, fill");

		final JTextArea textArea_1 = new JTextArea();
		scrollPane.setViewportView(textArea_1);
		textArea_1.setRows(15);
		textArea_1.setColumns(80);

		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, "2, 4, 5, 1, left, top");
		panel.setLayout(new GridLayout(0, 1, 0, 0));

		JLabel lblTo = new JLabel("to");
		frame.getContentPane().add(lblTo, "2, 6");

		JScrollPane scrollPane_1 = new JScrollPane();
		frame.getContentPane().add(scrollPane_1, "4, 6, fill, fill");

		final JTextArea textArea = new JTextArea();
		scrollPane_1.setViewportView(textArea);
		textArea.setColumns(80);
		textArea.setRows(15);

		JButton btnNewButton = new JButton("Parse");
		frame.getContentPane().add(btnNewButton, "4, 8");
		btnNewButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String text = textArea_1.getText();
				text = text.replace("\n", "\r\n");
				IParser parser = new DefaultParser(text);
				SentenceLexer lexer = new SentenceLexer(parser);
				try {
					String text2 = lexer.lex();
					textArea.setText(text2);
				} catch (ParserException e1) {
					e1.printStackTrace();
				}
			}
		});
	}

}
